package cn.jekun.web.shop.action;

import java.util.HashMap;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.core.Constants;
import cn.jekun.web.shop.bean.Cart;
import cn.jekun.web.shop.service.CartService;


@SuppressWarnings("serial")
public class CartAction extends ShopBaseAction{
	@Resource(name = CartService.BEAN_NAME)
	CartService cartService;
	
	/**
	 * 购物车查询列表
	 * 
	 * @return
	 */
	public String getCartList() {

		JSONObject jsonObject = new JSONObject();

		try {
			String mobile = request.getParameter("mobile");
			String merchandiseName = request.getParameter("merchandiseName");
			String shopId = this.getSessionUser().getShopId();
			this.resultList = this.cartService.getCartList(mobile,merchandiseName, shopId,page);

			JSONArray jsonArray = new JSONArray();

			HashMap<String, Object> hashMap = new HashMap<String, Object>();

			for (int i = 0; i < this.resultList.size(); i++) {

				Cart cart = (Cart) resultList.get(i);

				hashMap.put(Cart.COL_CART_ID, cart.getCartId());

				hashMap.put("merchandise_name", cart.getMerchandise_name() == null ? "" : cart.getMerchandise_name());
				
				hashMap.put("barcode", cart.getBarcode() == null ? "" : cart.getBarcode());
				
				hashMap.put("shop_name", cart.getShop_name() == null ? "" : cart.getShop_name());
				
				hashMap.put("mobile", cart.getMobile() == null ? "" : cart.getMobile());
				
				hashMap.put(Cart.COL_CREATE_TIME, cart.getCreateTime() == null ? "" : cart.getCreateTime());
				
				jsonArray.add(hashMap);

			}

			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

			jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");

			jsonObject.put(Constants.Page.PAGE_NUMBER, page.getPageNumber());

			jsonObject.put(Constants.Page.PAGE_SIZE, page.getPageSize());

			jsonObject.put(Constants.Page.TOTAL_COUNT, page.getTotalCount());

			jsonObject.put(Constants.Page.TOTAL_PAGE, page.getPages());

			jsonObject.put("cartListData", jsonArray);

		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
}
